package com.example.tbclientdemo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.example.tbclientdemo.mapper.ElecMeterDataMapper;
import com.example.tbclientdemo.model.MeterData;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

@Component
@Slf4j
public class DataLoadService {

    @Autowired
    private ElecMeterDataMapper elecMeterDataMapper;

    @PostConstruct
    private void init() {

    }



//    @EventListener(ApplicationReadyEvent.class)
//    @Async
    void loadToTDEngine(){


//        LocalDateTime startDate = LocalDateTime.of(2023, 1, 1, 0, 0, 0);
//
//        double totalPower = 0.0;
//
//        while (startDate.isBefore(LocalDateTime.now())){
//            startDate =  startDate.plusDays(1);
//
//            totalPower = totalPower + RandomUtils.nextDouble(0, 10);
//
//            elecMeterDataMapper.insertElecMeterData(convertToDate(startDate),"test",totalPower);
//
//        }
//
//        log.info("Finished.");


        try {
            File file = ResourceUtils.getFile("classpath:excel/n0.xls");

            EasyExcel.read(file, MeterData.class, new ReadListener<MeterData>() {

                private List<MeterData> cache = new ArrayList<>();

                @Override
                public void invoke(MeterData data, AnalysisContext context) {
                    cache.add(data);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                    cache.forEach(new Consumer<MeterData>() {
                        @Override
                        public void accept(MeterData meterData) {
                            elecMeterDataMapper.insertElecMeterData(meterData.getTime(),"test",meterData.getTotalPower());
                        }
                    });

                }
            }).headRowNumber(5).sheet().doRead();

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        log.info("Finished.");
    }

    private  Date convertToDate(LocalDateTime localDateTime) {
        ZoneId defaultZoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(defaultZoneId);
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }


}
